package com.sls.mall.member.service.impl;

import com.sls.mall.member.dao.MemberLevelDao;
import com.sls.mall.member.entity.MemberLevelEntity;
import com.sls.mall.member.exception.PhoneExsitException;
import com.sls.mall.member.exception.UsernameExistException;
import com.sls.mall.member.vo.MemberLoginVO;
import com.sls.mall.member.vo.MemberRegistVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sls.mall.common.utils.PageUtils;
import com.sls.mall.common.utils.Query;

import com.sls.mall.member.dao.MemberDao;
import com.sls.mall.member.entity.MemberEntity;
import com.sls.mall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVO memberRegistVo) {

        MemberDao memberDao = this.baseMapper;

        MemberEntity memberEntity = new MemberEntity();

        //设置默认等级
        MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
        memberEntity.setLevelId(levelEntity.getId());

        //检测手机号及用户名是否唯一
        // 不报异常即为通过
        checkPhoneUnique(memberRegistVo.getPhone());
        checkUsernameUnique(memberRegistVo.getUserName());



        memberEntity.setMobile(memberRegistVo.getPhone());
        memberEntity.setUsername(memberRegistVo.getUserName());

        //密码 md5 盐值加密 BCryptPasswordEncoder 好处是 盐值不需要存储
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        //盐值加密
        String encode = passwordEncoder.encode(memberRegistVo.getPassword());
        //解密 参数：1 明文密码  2 数据库存储的加密密码
//        passwordEncoder.matches(memberRegistVo.getPassword(),encode);

        memberEntity.setPassword(encode);

        memberDao.insert(memberEntity);

    }

    public void checkUsernameUnique(String userName) throws UsernameExistException {

        MemberDao memberDao = this.baseMapper;

        Integer username = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));

        if (username > 0)
        {
            throw new UsernameExistException();
        }


    }

    public void checkPhoneUnique(String phone) throws PhoneExsitException {

        MemberDao memberDao = this.baseMapper;

        Integer mobile = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));

        if (mobile > 0)
        {
            throw new UsernameExistException();
        }


    }

    @Override
    public MemberEntity login(MemberLoginVO memberLoginVO) {

        String loginacct = memberLoginVO.getLoginacct();

        String password = memberLoginVO.getPassword();

        MemberDao baseMapper = this.baseMapper;

        MemberEntity entity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginacct).or().eq("mobile", loginacct));

        if (entity == null)
        {
            // 登录失败
            return null;
        }else {
            // 数据库获取密码
            String passwordDB = entity.getPassword();

            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            // 匹配密码
            boolean matches = passwordEncoder.matches(password, passwordDB);
            if (matches)
            {
                return entity;
            }else {
                return null;
            }


        }

    }

}